<?php
namespace Addons\Wukong\Model;
use Think\Model;


class WukongModel /*extends Model*/{


    public  $_token = '';
    public $_domain = '';
    public $_appkey = array();
    public $_appsecret = array();

   // public $_host = 'https://sandbox-wkapi.laiwang.com';
    public $_host = 'https://wkapi.laiwang.com';

    public function __construct(){
        $config = get_addon_config('Wukong');

        $this->_token = $config['token'];
        $this->_domain = $config['domain'];

        $this->_appkey['web'] = $config['app_key'];
        $this->_appsecret['web'] = $config['app_secret'];
    }

    /**
     * description: 验证http Authorization 签名是否正确
     * @param httpAuth  http Authorization头
     * @param appToken  应用的token
     * @param appDomain 应用的domain
     * @return false 验签失败 ; true 验签成功
     */
    public function checkSha1Sign($authorization)
    {
        echo "enter into checkSha1Sign\n";
        //e.g. authorization的值类似如下:
        //Wukong nonce="73165416", domain="demo", timestamp="1425611635", signature_method="sha1", version="1.0", signature="bc9ddc1b264a7709042f3329fca99b8fbb393863"
        list($protocol, $args) = explode(" ", $authorization, /*limit*/2);
        $params = array();
        foreach (explode(",", trim($args)) as $item) {
            list($key, $value) = explode("=", $item, /*limit*/2);
            $params[trim($key)] = trim($value, " \t\n\r\"");
        }

        $signature = $params["signature"];
        $timestamp = $params["timestamp"];
        $nonce = $params["nonce"];

        $tmpArr = array($this->_token, $timestamp, $nonce);
        sort($tmpArr, SORT_STRING);
        $tmpStr = implode($tmpArr);
        $expect = sha1($tmpStr);

        if ($expect == $signature) {
            echo "checkSha1Sign true\n";
            return true;
        } else {
            echo "checkSha1Sign false\n";
            return false;
        }
    }

    /**
     * description: 根据domain 和token 生成 http Authorization 签名
     * @param domain   应用申请的domain
     * @param token    应用申请的token
     * @return String  http Authorization
     */
    public function getSha1Sign()
    {
        $nonce = mt_rand(100000,200000);
        $timestamp = time();
        $signature_array=array(
            $this->_token,
            (string)$nonce,
            (string)$timestamp,
        );
        sort($signature_array, SORT_STRING);
        $signature= sha1(implode($signature_array));
        return "Wukong nonce=\"{$nonce}\", domain=\"{$this->_domain}\", timestamp=\"{$timestamp}\", signature_method=\"sha1\", version=\"1.0\", signature=\"{$signature}\"";
    }


    /**
     * visitUrl 访问网站
     * @param $url
     * @param $type
     * @param array $params
     * @return array
     * @author:xjw129xjt(肖骏涛) xjt@ourstu.com
     */
    public function visitUrl($url,$type,$params=array()){

        $signature = $this->getSha1Sign();
        $_headers = array('Content-type: application/json');
        array_push($_headers, 'Authorization: ' . $signature);


        $params = is_array($params)?http_build_query($params):$params;
        $ch = curl_init();
        $timeout = 30;
        curl_setopt ($ch, CURLOPT_URL, $url);

        $signature = $this->getSha1Sign();
        $_headers = array('Content-type: application/json');
        array_push($_headers, 'Authorization: ' . $signature);
        curl_setopt ($ch, CURLOPT_HTTPHEADER, $_headers);

        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);

        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);

        switch ($type){
            case "GET" : curl_setopt($ch, CURLOPT_HTTPGET, true);break;
            case "POST": curl_setopt($ch, CURLOPT_POST,true);
                curl_setopt($ch, CURLOPT_POSTFIELDS,$params);break;
            case "PUT" : curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, "PUT");
                curl_setopt($ch, CURLOPT_POSTFIELDS,$params);break;
            case "DELETE":curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
                curl_setopt($ch, CURLOPT_POSTFIELDS,$params);break;
        }
        $file_contents = curl_exec($ch);//获得返回值
        $return_code = curl_getinfo ( $ch, CURLINFO_HTTP_CODE );

        curl_close($ch);
        return array($return_code,$file_contents);
    }
	
}